package com.xx.controller;

import com.xx.comm.R;
import com.xx.entity.Category;
import com.xx.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "菜品分类管理")
@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
@CrossOrigin
public class CategoryController {

    @Autowired
    ICategoryService categoryService;
    // 新增
    @PostMapping
    @Operation(summary = "新增分类",description = "新增分类接口")
    public R insert(@Parameter(description = "分类信息") @RequestBody Category category){
        return categoryService.insert(category);
    }

    // 删除
    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类",description = "删除分类接口")
    public R delete(@Parameter(description = "分类id") @Validated @PathVariable("id") Integer id){
        return categoryService.delete(id);
    }

    // 修改
    @PutMapping
    @Operation(summary = "修改分类",description = "分类商品接口")
    public R update(@Parameter @RequestBody Category category){
        return R.success(categoryService.update(category));
    }

    // 查询
    @GetMapping()
    @Operation(summary = "查询分类",description = "查询所有和根据名称查询分类接口")
    public R selectById(@Parameter String name,  @Parameter Integer page
            ,@RequestParam Integer size){
        if ("all".equals(name)) {
            return categoryService.selectAll(page,size);
        }else {
            return categoryService.selectByName(name,page,size);
        }
    }
}
